package com.ruoyi.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.IUserTaskLikeService;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 用户任务点赞/关注Controller
 *
 * @author ruoyi
 * @date 2024-07-03
 */
@RestController
@RequestMapping("/system/task/like")
public class UserTaskLikeController extends BaseController
{
    @Autowired
    private IUserTaskLikeService userTaskLikeService;

    /**
     * 切换任务点赞状态（点赞/取消点赞）
     */
    @PostMapping("/toggle/{taskId}")
    public AjaxResult toggleLike(@PathVariable Long taskId)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        if (userId == null) {
            return AjaxResult.error("用户未登录");
        }
        int result = userTaskLikeService.toggleTaskLike(userId, taskId);
        if (result == 1) {
            return AjaxResult.success("点赞成功", result);
        } else {
            return AjaxResult.success("取消点赞成功", result);
        }
    }

    /**
     * 查询用户是否已点赞某个任务
     */
    @GetMapping("/check/{taskId}")
    public AjaxResult checkLikeStatus(@PathVariable Long taskId)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        if (userId == null) {
            return AjaxResult.error("用户未登录");
        }
        boolean liked = userTaskLikeService.checkUserLikedTask(userId, taskId);
        return AjaxResult.success(liked ? "已点赞" : "未点赞", liked);
    }
} 